package at.fhhgb.mc.android.mfb_hgb.activity;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import at.fhhgb.mc.android.mfb_hgb.R;

public class LoginActivity extends Activity implements TabListener {

	private ActionBar ab;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);

		ab = getActionBar();
//		ab.setDisplayShowTitleEnabled(false);
//		ab.setDisplayShowHomeEnabled(false);
		ab.setTitle(getString(R.string.app_name));
		setNavigationTabs();
		ab.show();
	}

	private void setNavigationTabs() {
		ab.removeAllTabs();
		ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		// Tabs
		Tab tab1 = ab.newTab().setText(getString(R.string.login)).setTabListener(this);
		Tab tab2 = ab.newTab().setText(getString(R.string.register)).setTabListener(this);

		// Add Tabs
		ab.addTab(tab1);
		ab.addTab(tab2);
	}

	@Override
	public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
		FragmentManager fm = getFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();

		switch (arg0.getPosition()) {
		case 0:
			ft.replace(R.id.fragmentContainer, new LoginFragment());
			break;

		case 1:
			ft.replace(R.id.fragmentContainer, new RegisterFragment());
			break;
		}

		ft.commit();
	}

	@Override
	public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onBackPressed() {
		FragmentManager fm = getFragmentManager();
		if (fm.getBackStackEntryCount() > 0) {
			fm.popBackStack();
		} else {
			super.onBackPressed();
		}
	}
}
